package cn.kgc.controller;

import cn.kgc.common.CrowdFundingConstant;


import cn.kgc.common.ResultEntity;
import cn.kgc.pojo.Hmold;
import cn.kgc.pojo.Houserole;
import cn.kgc.service.HouseroleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class Housecontroller {
    @Resource
    HouseroleService houseroleService;

    @RequestMapping("/houserole/query/for/search")
    public String queryForSearch(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            Model model) {
        PageInfo<Houserole> pageInfo = houseroleService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "body/houserole/member-list2";
    }



    //详细信息            houseMold
    @RequestMapping("/houserole/By/id")
    public String selectid(@RequestParam("id") Integer id, Model model) {
        Houserole xiangqing =houseroleService.selectByIdUser(id);
        model.addAttribute("xiang", xiangqing);
        return "body/houserole/member-xiangxi";
    }


    //删除回显 批量
    //根据id查询
    @ResponseBody      //mold
    @RequestMapping("/houserole/get/list/by/id/list")
    public ResultEntity<List<Houserole>> getRoleListByIdList(@RequestBody List<Integer> userIdList) {
        List<Houserole> userListByIdList = houseroleService.getUserListByIdList(userIdList);
        return ResultEntity.successWithoutData(userListByIdList);
    }

    //根据id删除  批量
    @ResponseBody   //mold
    @RequestMapping("/houserole/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> userIdList) {
        houseroleService.batchRemove(userIdList);
        return ResultEntity.successWithoutData();
    }

    //单条删除
    @RequestMapping("delete/houserole")
    public String deleteblog(@RequestParam("id") Integer id, Integer pageNum) {
        houseroleService.remove(id);
        return "redirect:/houserole/query/for/search";
    }

    //增加
    @RequestMapping("/houserole/save")
    public String saveAdmin(Hmold user, Integer pageNum) {
       houseroleService.saveUser(user);
        //删除成功跳转到查询页面  分页查询
        return "redirect:/houserole/query/for/search";
    }


    //详细信息
    @RequestMapping("/holds/id")
    public String selectids(@RequestParam("id") Integer id, Model model) {
        Houserole xiangqing = houseroleService.selectByIdUser(id);
        model.addAttribute("xiang", xiangqing);
        return "body/houserole/mold-update";
    }



    //修改
    @RequestMapping("/holds/update")
    public String updateAdmin(Hmold user, Integer pageNum) {
        houseroleService.updateUser(user);
        //当前页 存储过去
        return "redirect:/houserole/query/for/search";
    }


}
